function actionGetMe() {
    var token = Lib.getCookie('access_token');
    if(token == '') {
        return true;
    }
    var callback = function(xmlhttp) {
        var user = JSON.parse(xmlhttp.responseText);
        var span = document.getElementById('username');
        while( span.firstChild ) {
            span.removeChild( span.firstChild );
        }
        span.appendChild( document.createTextNode(user.name) );
        var span = document.getElementById('uploaded');
        while( span.firstChild ) {
            span.removeChild( span.firstChild );
        }
        span.appendChild( document.createTextNode(formatFileSize(user.uploaded)) );
        var span = document.getElementById('downloaded');
        while( span.firstChild ) {
            span.removeChild( span.firstChild );
        }
        span.appendChild( document.createTextNode(formatFileSize(user.downloaded)) );
    }
    Lib.http('get', '/api/me', {'access_token' : token}, callback, false);
    return true;
}
function formatFileSize(byte_size)
{
    if(parseInt(byte_size) > 1073741824) {
        return (byte_size/1073741824).toFixed(1) + 'GiB';
    }
    if(parseInt(byte_size) > 1048576) {
        return parseInt(byte_size/1048576) + 'MiB';
    }
    return parseInt(byte_size/1024) + 'KiB';
}
function attachEventDisplayLoginOrLogout()
{
    var token = Lib.getCookie('access_token');
    if(token == '') {
        document.getElementById('logout').style.display = 'none';
        document.getElementById('login').style.display = 'block';
    } else {
        document.getElementById('logout').innerHTML = '<ul> <li>↑ <span id="uploaded"></span> ↓ <span id="downloaded"></span></li> <li><a href="#" id="show_notifications">提醒<span id="notification_count"></span></a></li> <li><a href="' + 
            Conf.uri.accounts +
            '" target="_blank"><span id="username"></span>的帐号</a></li> <li><a href="' + 
            Conf.uri.logout +
            '">退出</a></li> </ul>';
        actionGetMe();
        document.getElementById('login').style.display = 'none';
        document.getElementById('logout').style.display = 'block';
    }
}
function attachEventGetBonus()
{
    var token = Lib.getCookie('access_token');
    if(token == '') {
        return true;
    }
    var callback = function(xmlhttp) {
        var user = JSON.parse(xmlhttp.responseText);
        if(user.is_today_logined == 1) {
            return true;
        }
        var FilesArray = [
            'http://html5test.com/translation.js',
            'http://html5test.com/scripts/useragents.php?zlkkpozq',
            'http://html5test.com/scripts/engine.js',
            'http://html5test.com/scripts/data.js',
            'http://html5test.com/scripts/template.js',
            'http://html5test.com/scripts/reporting.js',
        ];
        Lib.loadFileList(FilesArray, 'js', function(){
                actionGetBonus();
            }
        );
    }
    Lib.http('get', '/api/me', {'access_token' : token}, callback, true);
    return true;
}
function actionGetBonus()
{
    Browsers = new UserAgents(navigator.userAgent, {
        useFeatures:            true,
        detectCamouflage:       true
    });

    new Test(function(r) {
        
        /* Submit results */
        var data = {
            "score": r.score
        };
        
        var callback = function(xmlhttp) {
            var r = JSON.parse(xmlhttp.responseText);
            actionCountUnreadedNotifications();
            attachEventGetNotifications();
        }
        Lib.http('post', '/api/user_agent', data, callback, false);
    });
}
function actionCountUnreadedNotifications()
{
    var token = Lib.getCookie('access_token');
    if(token == '') {
        return true;
    }
    var callback = function(xmlhttp) {
        var r = JSON.parse(xmlhttp.responseText);
        if(r.count == 0) {
            document.getElementById('notification_count').innerHTML = '';
        } else {
            document.getElementById('notification_count').innerHTML = '(' + r.count + ')';
        }
    }
    Lib.http('get', '/api/count_unreaded_notifications', {'access_token' : token}, callback, true);
    return true;
} 
function attachEventGetNotifications()
{
    var token = Lib.getCookie('access_token');
    if(token == '') {
        return true;
    }
    var callback = function(xmlhttp) {
        var r = JSON.parse(xmlhttp.responseText);
        actionSetNotificationsToHtml(r);
    }
    Lib.http('get', '/api/notifications', {'access_token' : token}, callback, true);
    return true;
}
function actionSetNotificationsToHtml(data)
{
    var html = '<ul>';
    for(var i=0, l=data.length; i<l; i++) {
        var d = new Date(parseInt(data[i].create_time) * 1000);
        var hour = d.getHours();
        hour = hour < 10 ? '0' + hour : hour;
        var min = d.getMinutes();
        min = min < 10 ? '0' + min : min;
        html += '<li>' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + hour + ':' + min + ' ';
        if(parseInt(data[i].action) == 1) { //todo hard code
            html += '<a href="http://html5test.com/" target="_blank">' + data[i].content + '</a></li>';
        } else {
            html += data[i].content + '</li>';
        }
    }
    html += '</ul>';
    document.getElementById('notification').innerHTML = html;
}
function attachEventShowNotifications()
{
    document.getElementById('show_notifications').onclick = function() {
        actionShowNotifications();
        return false;
    }
}
function actionShowNotifications()
{
    var data = document.getElementById('notification').getAttribute('data-display');
    if(data == null || data == 'none') {
        attachEventGetNotifications();
        document.getElementById('notification').setAttribute('data-display', 'block');
        document.getElementById('notification').style.display = 'block';
        if(document.getElementById('notification_count').innerHTML != '') {
            actionMarkReadedNotifications();
            document.getElementById('notification_count').innerHTML = '';
        }
    } else {
        document.getElementById('notification').setAttribute('data-display', 'none');
        document.getElementById('notification').style.display = 'none';
    }
    return true;
}
function actionMarkReadedNotifications()
{
    var token = Lib.getCookie('access_token');
    if(token == '') {
        return true;
    }
    var callback = function(){};
    Lib.http('post', '/api/readed_notifications', {'access_token' : token}, callback, true);
    return true;
}
